package com.renwei.yunlong.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.work.ServiceSelectActivity;
import com.renwei.yunlong.activity.work.SpareSelectActivity;
import com.renwei.yunlong.activity.work.WorkDateSelecterActivity;
import com.renwei.yunlong.activity.work.WorkSpareObjectSelectActivity;
import com.renwei.yunlong.base.BaseFragment;
import com.renwei.yunlong.bean.WorkSpareBean;
import com.renwei.yunlong.global.AppHelper;

import java.util.HashMap;

import butterknife.Bind;

import static android.app.Activity.RESULT_OK;

/**
 * Created by wangqi on 2017/11/24.
 */

public class WorkSpareDetailFragment extends BaseFragment implements View.OnClickListener,
        RadioGroup.OnCheckedChangeListener, TextWatcher {

    @Bind(R.id.rb_purpose_left)
    RadioButton rbPurposeLeft;
    @Bind(R.id.rb_purpose_right)
    RadioButton rbPurposeRight;
    @Bind(R.id.rg_purpose)
    RadioGroup rgPurpose;
    @Bind(R.id.tv_object)
    TextView tvObject;
    @Bind(R.id.rl_object)
    RelativeLayout rlObject;
    @Bind(R.id.tv_service)
    TextView tvService;
    @Bind(R.id.rl_service)
    RelativeLayout rlService;
    @Bind(R.id.tv_third)
    TextView tvThird;
    @Bind(R.id.rl_third)
    RelativeLayout rlThird;
    @Bind(R.id.tv_spare)
    TextView tvSpare;
    @Bind(R.id.rl_spare)
    RelativeLayout rlSpare;
    @Bind(R.id.rb_replace_left)
    RadioButton rbReplaceLeft;
    @Bind(R.id.rb_replace_right)
    RadioButton rbReplaceRight;
    @Bind(R.id.rg_replace)
    RadioGroup rgReplace;
    @Bind(R.id.rl_replace)
    RelativeLayout rlReplace;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.rl_time)
    RelativeLayout rlTime;
    @Bind(R.id.tv_input_title)
    TextView tvInputTitle;
    @Bind(R.id.et_input_content)
    EditText etInputContent;
    @Bind(R.id.tv_input_number)
    TextView tvInputNumber;
    private String requestId;
    private String hardwareId;
    private String eventId;
    private String contractId;
    private WorkSpareBean workSpareBean;
    private String relationId;

    public WorkSpareDetailFragment(String requestId, String eventId, String contractId, WorkSpareBean workSpareBean) {
        this.requestId = requestId;
        this.eventId = eventId;
        this.contractId = contractId;
        this.workSpareBean = workSpareBean;
    }

    private String spareId;
    private String arriveTimeGet;
    private String oldReturnFlag;
    private String isTurnFlag = "无";
    private String typeFlag;

    public HashMap<String, String> getSpareInfo() {
        HashMap<String, String> map = new HashMap<>();
        map.put("spareId", spareId);
        map.put("arriveTimeGet", arriveTimeGet);
        map.put("title", etInputContent.getText().toString());
        map.put("hardwareId", hardwareId);
        map.put("requestId", requestId);
        map.put("typeFlag", typeFlag);
        if ("2".equals(companyType)) {
            map.put("isTurnFlag", isTurnFlag);
            map.put("relationId", relationId);
        }
        if ("2".equals(typeFlag)) {
            map.put("oldReturnFlag", oldReturnFlag);
        }
        return map;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_work_spare_detail;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {

        rlObject.setOnClickListener(this);
        rlSpare.setOnClickListener(this);
        rlTime.setOnClickListener(this);
        rlService.setOnClickListener(this);

        tvInputTitle.setText("申请理由(必填)");
        etInputContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(100), AppHelper.emojiFilter});
        rgPurpose.setOnCheckedChangeListener(this);
        rgReplace.setOnCheckedChangeListener(this);
        etInputContent.addTextChangedListener(this);

        rbPurposeLeft.toggle();
        rbReplaceLeft.toggle();

        if ("1".equals(companyType)) {//业主
            rlThird.setVisibility(View.GONE);
            rlService.setVisibility(View.GONE);
        } else if ("2".equals(companyType)) {//服务商
            rlThird.setVisibility(View.VISIBLE);
            rlService.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle bundle = null;
            if (data != null) {
                bundle = data.getExtras();
            }
            if (requestCode == 100) {
                tvObject.setText(bundle.getString("objectName"));
                hardwareId = bundle.getString("objectId");

                tvService.setText("");
                relationId = "";
            }
            if (requestCode == 111) {
                if (!TextUtils.isEmpty(bundle.getString("hardName"))) {
                    tvSpare.setText(bundle.getString("hardName"));
                }
                spareId = bundle.getString("spareId");
            }
            if (requestCode == 222) {
                tvTime.setText(bundle.getString("time"));
                arriveTimeGet = bundle.getString("time");
            }
            if (requestCode == 333) {
                tvService.setText(bundle.getString("detailName"));
                relationId = bundle.getString("relationId");
                if (TextUtils.isEmpty(relationId)) {
                    isTurnFlag = "无";
                    tvThird.setText("无");
                } else {
                    isTurnFlag = "有";
                    tvThird.setText("有");
                }
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_object:
                WorkSpareObjectSelectActivity.OpenActivity(this, hardwareId, workSpareBean, 100);
                break;
            case R.id.rl_spare:
                startActivityForResult(new Intent(getContext(), SpareSelectActivity.class), 111);
                break;
            case R.id.rl_time:
                WorkDateSelecterActivity.OpenActivityForResult(this, requestId, eventId, "到场时间", 222);
                break;
            case R.id.rl_service: //服务目录
                if (TextUtils.isEmpty(hardwareId)) {
                    showCenterInfoMsg("请选择对象");
                } else {
                    ServiceSelectActivity.OpenActivity(this, ServiceSelectActivity.FROM_WORK_SPARE, contractId, hardwareId, "", "", 333);
                }
                break;
            default:
            break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int id) {
        switch (id) {
            case R.id.rb_purpose_left:
                rlReplace.setVisibility(View.GONE);
                typeFlag = "1";
                break;
            case R.id.rb_purpose_right:
                rlReplace.setVisibility(View.VISIBLE);
                typeFlag = "2";
                break;
            case R.id.rb_replace_left: // 入库
                oldReturnFlag = "1";
                break;
            case R.id.rb_replace_right:  // 不处理
                oldReturnFlag = "2";
                break;
                default:
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        int length = s.length();
        tvInputNumber.setText(length + "");
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
}
